package top.jiangqiang.qyblog.admin.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import top.jiangqiang.qyblog.common.base.controller.BaseController;
import top.jiangqiang.qyblog.common.base.query.PageResult;
import top.jiangqiang.qyblog.common.base.response.Result;
import top.jiangqiang.qyblog.common.constant.RequestURLConstants;
import top.jiangqiang.qyblog.core.domain.dto.CategoryAddDto;
import top.jiangqiang.qyblog.core.domain.dto.CategoryQueryDto;
import top.jiangqiang.qyblog.core.domain.dto.CategoryUpdateDto;
import top.jiangqiang.qyblog.core.domain.vo.CategoryVo;
import top.jiangqiang.qyblog.core.service.ICategoryService;

/**
 * @author jiangqiang
 * @date 2022-11-27
 */
@RequiredArgsConstructor
@RestController
@RequestMapping(RequestURLConstants.CATEGORY_CONTROLLER_PREFIX)
@Tag(name = "文章分类接口")
public class CategoryController extends BaseController {
    private final ICategoryService categoryService;

    @GetMapping
    @Operation(summary = "根据条件查询分类列表", description = "指定父级分类id后，只查询该分类下的直接子分类")
    public Result<PageResult<CategoryVo>> list(@ParameterObject CategoryQueryDto categoryQueryDto) {
        return categoryService.findByCondition(categoryQueryDto);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询分类详情", description = "根据id查询分类")
    public Result<CategoryVo> get(@PathVariable Long id) {
        return categoryService.getCategory(id);
    }

    @PostMapping
    @Operation(summary = "新增分类", description = "新增分类")
    public Result<CategoryVo> save(@RequestBody CategoryAddDto categoryAddDto) {
        return categoryService.saveCategory(categoryAddDto);
    }

    @PutMapping
    @Operation(summary = "编辑分类", description = "根据id修改分类")
    public Result<CategoryVo> update(@RequestBody CategoryUpdateDto categoryUpdateDto) {
        return categoryService.editCategory(categoryUpdateDto);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类", description = "根据id删除分类，会删除该分类下的直接或间接子分类")
    public Result<Boolean> delete(@PathVariable Long id) {
        return categoryService.deleteCategory(id);
    }
}
